package com.coinland.study.util;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * 文件工具类
 */
public class FileUtil {


    /**
     * 获取文件大小
     *
     * @param size
     * @return
     */
    public static final String getFileSize(final long size) {
        if (size > 1073741824) {
            return String.format("%.2f", size / 1073741824.0) + " GB";
        } else if (size > 1048576) {
            return String.format("%.2f", size / 1048576.0) + " MB";
        } else if (size > 1024) {
            return String.format("%.2f", size / 1024.0) + " KB";
        } else {
            return size + " B";
        }
    }

    /**
     * 判断是否有SD卡
     *
     * @return
     */
    public static boolean hasSdcard() {
        boolean bool = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());

        return bool;
    }


    public static void writeFile(InputStream inputString, File file) throws IOException {
        if (file.exists()) {
            file.delete();
        }

        FileOutputStream fos = null;

        fos = new FileOutputStream(file);

        byte[] b = new byte[1024];

        int len;
        while ((len = inputString.read(b)) != -1) {
            fos.write(b, 0, len);
        }
        inputString.close();
        fos.close();
    }


}
